﻿#region 文件信息

// FileName: SoCafeShop/Sohobloo.SoAuth.Console/Program.cs
// Author: Sohobloo
// CreateTime: 2013/06/28 20:31
// UpdateTime: 2013/06/30 3:41

#endregion

#region 引用

using System;
using System.Collections.ObjectModel;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.IO;
using System.Reflection;
using Sohobloo.SoAuth.Data.Initialize;
using Sohobloo.SoAuth.Data.Repositories;
using Sohobloo.SoAuth.Entity;
using Sohobloo.SoAuth.Service;

#endregion

namespace Sohobloo.SoAuth.Console
{
    [Export]
    internal class Program
    {
        private static CompositionContainer _container;

        [Import]
        protected IFunctionService FunctionService { get; private set; }

        [Import]
        protected IFunctionRepository FunctionRepository { get; private set; }

        private static void Main(string[] args)
        {
            var catalog = new AggregateCatalog();
            catalog.Catalogs.Add(new DirectoryCatalog(Directory.GetCurrentDirectory()));
            catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
            _container = new CompositionContainer(catalog);


            DbInitializer.Initialize();
            Method01();
            Method02();
            Method03();
            System.Console.ReadLine();
        }

        private static void Method01()
        {
            var program = _container.GetExportedValue<Program>();

            var functionParam = new SoAuthFunctionParam {ParamName = "type", ParamValue = "1"};
            var functionAction = new SoAuthAction {ActionName = "登入", ActionPath = "Login"};
            var function = new SoAuthFunction
                           {
                               Id = new Guid("87D0A0D2-F59B-4141-B564-2E1B41EA9F41"),
                               FunctionEnabled = true,
                               FunctionName = "首页",
                               FunctionPath = "Home",
                               IgnoreControl = true,
                               InMenu = true,
                               MenuLevel = 1,
                               FunctionParams = new Collection<SoAuthFunctionParam> {functionParam},
                               FunctionActions = new Collection<SoAuthAction> {functionAction}
                           };

            var result = program.FunctionService.CreateFunction(function);
            System.Console.WriteLine("ResultType: {0}, Message: {1}", result.ResultType, result.Message);
        }

        private static void Method02()
        {
            var program = _container.GetExportedValue<Program>();

            var functionId = new Guid("87D0A0D2-F59B-4141-B564-2E1B41EA9F41");
            var function = program.FunctionRepository.GetByKey(functionId);
            function.FunctionEnabled = !function.FunctionEnabled;
            var result = program.FunctionService.UpdateFunction(function);
            System.Console.WriteLine("ResultType: {0}, Message: {1}", result.ResultType, result.Message);
        }

        private static void Method03()
        {
            var program = _container.GetExportedValue<Program>();

            var functionId = new Guid("87D0A0D2-F59B-4141-B564-2E1B41EA9F41");
            var result = program.FunctionService.DeleteFunction(functionId);
            System.Console.WriteLine("ResultType: {0}, Message: {1}", result.ResultType, result.Message);
        }


    }
}